package com.ddtech.framework.system.repository;

import com.ddtech.framework.system.model.SysDept;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Set;

/**
* @author Apache License
* @date 2019-03-25
*/
public interface DeptRepository extends JpaRepository<SysDept, String>, JpaSpecificationExecutor {

    /**
     * findByPid
     * @param id
     * @return
     */
    List<SysDept> findByPid(String id);

    @Query(value = "select name from sys_dept where id = ?1",nativeQuery = true)
    String findNameById(String id);

    Set<SysDept> findByRoles_Id(String id);

    /**
     * 子查父
     * @return
     */
    @Query(value = "SELECT  * FROM SYS_DEPT START WITH id = ?1  CONNECT BY PRIOR pid = id",nativeQuery = true)
    List<SysDept>  findDeptTreeById(String deptId);
}